package com.testing.class7;

import com.testing.web.WebKeyword;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CasesReadTest
 * @Description 类型说明
 * @Date 2022/10/19 20:17
 * @Created by 特斯汀Roy
 */
public class CasesReadTest {
    public static void main(String[] args) throws IOException {
        WebKeyword web=new WebKeyword();
        //创建一个文件输入流，将webcases.xlsx文件放到输入管里
        FileInputStream webCasesInput=new FileInputStream("Cases/WebCases.xlsx");
        //通过输入管将内容写到jvm的workbook对象里。
        Workbook webcasesBook=new XSSFWorkbook(webCasesInput);
        for (int sheetNo = 0; sheetNo < webcasesBook.getNumberOfSheets(); sheetNo++) {
            Sheet sheetNow = webcasesBook.getSheetAt(sheetNo);
            System.out.println(sheetNow.getSheetName());
            //对每个sheet页，遍历每一行
            for (int rowNO = 0; rowNO < sheetNow.getPhysicalNumberOfRows(); rowNO++) {
                Row row = sheetNow.getRow(rowNO);
                //把每一行存成一个list<String>
                List<String> rowContent=new ArrayList<>();
//                System.out.println(row);
                //遍历每一行
                for (int columnNo = 0; columnNo < row.getPhysicalNumberOfCells(); columnNo++) {
                    Cell cell = row.getCell(columnNo);
                    //虽然输出cell是单元格字符串内容是因为sout的时候，默认调用toString方法，但他的类型是cell不是string
//                    System.out.print(cell+",");
                    rowContent.add(cell.toString());
                    //读取第4列
//                    if(columnNo==3){
//                        //找到openbrowser的时候，把后面的参数，通过openbrowser方法传递调用。
//                        if(cell.toString().equals("openBrowser")){
//                            web.openBrowser(row.getCell(columnNo+1).toString());
//                        }else if(cell.toString().equals("visitWeb")){
//                            web.visitWeb(row.getCell(columnNo+1).toString());
//                        }
//                    }
                }
                System.out.println(rowContent);
                System.out.println("-------------------------------");
                //每一行的第10列都进行写入
                row.getCell(10).setCellValue("测试完成");
            }
        }
        //读取完成,关闭输入流
        webCasesInput.close();
        //想要写入文件中，创建输出流，进行写操作
        FileOutputStream outputStream=new FileOutputStream("Cases/WebCases.xlsx");
        webcasesBook.write(outputStream);
        //关闭输出流和book
        outputStream.close();
        webcasesBook.close();

//        Sheet 商城后台系统用例 = webcasesBook.getSheet("商城后台系统用例");
//        Row row = 商城后台系统用例.getRow(0);
//        Cell cell = row.getCell(0);
//        System.out.println(cell);
    }


}
